package org.jsbd.boss.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.jsbd.boss.common.DataGridModel;
import org.jsbd.boss.common.redis.RedisClient;
import org.jsbd.boss.dao.IBookDao;
import org.jsbd.boss.dao.IBookGroupDao;
import org.jsbd.boss.domian.BookGroup;
import org.jsbd.boss.service.IBookGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("bookGroupService")
public class BookGroupServiceImpl implements IBookGroupService {

	@Autowired
	private IBookGroupDao bookGroupDao;

	@Autowired
	private IBookDao bookDao;

	@Resource
	private RedisClient<String, String> redisClient;

	@Override
	public void save(BookGroup bookGroup) {
		bookGroupDao.save(bookGroup);
	}

	@Override
	public void save(List<BookGroup> list) {
		bookGroupDao.save(list);
	}

	@Override
	public int update(BookGroup bookGroup) {
		return bookGroupDao.update(bookGroup);
	}

	@Override
	public BookGroup getById(Integer id) {
		return bookGroupDao.getById(id);
	}

	@Override
	public List<BookGroup> getList(BookGroup bookGroup) {
		return bookGroupDao.getList(bookGroup);
	}

	@Override
	public Map<String, Object> getListPage(DataGridModel page, BookGroup bookGroup) {
		bookGroup = (bookGroup == null ? new BookGroup() : bookGroup);
		bookGroup.setPageInfo(page);
		Map<String, Object> results = new HashMap<String, Object>();
		results.put("total", bookGroupDao.getListPageCount(bookGroup));
		List<BookGroup> list = (List<BookGroup>) bookGroupDao.getList(bookGroup);
		for (BookGroup bk : list) {
			bk.setCreateDate(org.jsbd.boss.util.DateUtil.getYmdhmsDateStr(bk.getCreateTime()));
			bk.setModifyDate(org.jsbd.boss.util.DateUtil.getYmdhmsDateStr(bk.getModifyTime()));
		}
		results.put("rows", list);
		return results;
	}

	@Override
	public void delete(List<Integer> ids) {
		bookGroupDao.delete(ids);

		for (Integer bookGroupId : ids) {
			bookDao.deleteByBookGroupId(bookGroupId);
		}
	}

}
